package com.ly.algorithm.DataStructures.hashtable;

/**
 * @Classname HashTable
 * @Description TODO
 * @Date 2020/4/25 11:39
 * @Author 冷心影翼
 */
public class MyHashTable {
    private EmployeeLinkedList[] employeeLinkedListArray;

    private int size = 16;

    public MyHashTable() {
        this.employeeLinkedListArray = new EmployeeLinkedList[size];
        for(int i=0;i<size;i++) {
            employeeLinkedListArray[i] = new EmployeeLinkedList();
        }
    }

    public MyHashTable(int size) {
        this.size = size;
        this.employeeLinkedListArray = new EmployeeLinkedList[size];
        for(int i=0;i<size;i++) {
           employeeLinkedListArray[i] = new EmployeeLinkedList();
        }
    }

    public void add(Employee employee) {
        int arrayNo = hasFun(employee.id);
        employeeLinkedListArray[arrayNo].add(employee);
    }

    public void list() {
        for (int i = 0 ; i < size; i++) {
            employeeLinkedListArray[i].list();
        }
    }

    private int hasFun(int id) {
        return id % size;
    }

    public Employee findById(int id) {
        int arrayNo = hasFun(id);
        return employeeLinkedListArray[arrayNo].find(id);
    }

    public void deleteById(int id) {
        int arrayNo = hasFun(id);
        employeeLinkedListArray[arrayNo].delete(id);
    }
}

class Test {
    public static void main(String[] args) {
        MyHashTable hashTable = new MyHashTable();
        hashTable.add(new Employee(1,"a"));
        hashTable.add(new Employee(2,"b"));
        hashTable.add(new Employee(3,"c"));
        hashTable.add(new Employee(17,"c"));
//        hashTable.list();
//        System.out.println(hashTable.findById(1.txt));
//        System.out.println(hashTable.findById(111));

        hashTable.deleteById(17);
        hashTable.deleteById(1);
        hashTable.deleteById(2);
        hashTable.deleteById(3);
        hashTable.list();
    }
}

class Employee {
    public int id;

    public String name;

    public Employee next;

    public Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", next=" + next +
                '}';
    }
}

class EmployeeLinkedList {

    private Employee head;

    public boolean isEmpty() {
        if(head == null) {
            return true;
        }else {
            return false;
        }
    }
    public void add(Employee employee){
        if (head == null) {
            head = employee;
        }else {
            Employee cur = head;
            while (true) {
                if(cur.next == null) {
                    break;
                }
                cur = cur.next;
            }
            cur.next = employee;
        }
    }

    public void list() {
        if(isEmpty()) {

        }else {
            Employee cur = head;
            while (true) {
                System.out.print(cur+" ");
                if(cur.next==null) {
                    break;
                }
                cur = cur.next;
            }
            System.out.println();
        }

    }

    public Employee find(int id) {
        if(isEmpty()) {
            return null;
        }else {
            Employee cur = head;
            while (true) {
                if(cur.id == id) {
                    return cur;
                }
                if(cur.next == null) {
                    return null;
                }
                cur = cur.next;
            }
        }
    }

    public void delete(int id) {
        if(isEmpty()) {
            System.out.println("无法找到数据");
        }else {
            Employee cur = head;
            if(head.id == id) {
                head = head.next;
                return;
            }
            while (true) {
                if(cur.next == null) {
                    System.out.println("无法找到数据");
                    break;
                }
                if(cur.next.id == id) {
                    cur.next = cur.next.next;
                    break;
                }
                cur = cur.next;
            }
        }
    }
}